package com.shijiwei.lifthelper.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.shijiwei.lifthelper.R;
import com.shijiwei.lifthelper.adapter.CollectJokeAdapter;
import com.shijiwei.lifthelper.bean.DBJoke;
import com.shijiwei.lifthelper.ui.JokeInfoActivity;
import com.shijiwei.lifthelper.utils.DbUtils;

import org.xutils.common.util.LogUtil;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/17.
 */

@ContentView(R.layout.fragment_collect_joke)
public class CollectJokeFragment extends BaseFragment {

    @ViewInject(R.id.lv_collect_joke)
    private ListView jokeListView;

    private CollectJokeAdapter jokeAdapter;
    private List<DBJoke> list = new ArrayList<>();

    private MyReciver reciver;


    public static CollectJokeFragment newInstance() {
        CollectJokeFragment fragment = new CollectJokeFragment();
        return fragment;
    }

    @Override
    public void initView() {
        reciver = new MyReciver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("joke");
        getActivity().registerReceiver(reciver,filter);

        jokeAdapter = new CollectJokeAdapter(list,getActivity());
        jokeListView.setAdapter(jokeAdapter);

        jokeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), JokeInfoActivity.class);
                intent.putExtra("dbjoke", list.get(position));
                startActivity(intent);
            }
        });

        jokeListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                DBJoke dbJoke = list.get(position);
                try {
                    DbUtils.getDb().delete(dbJoke);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                list.remove(position);
                jokeAdapter.notifyDataSetChanged();
                return true;
            }
        });
    }

    @Override
    public void initData() {
        createData();
    }

    private void createData(){
        list.clear();
        try {
            List<DBJoke> dbJokes = DbUtils.getDb().findAll(DBJoke.class);
            if (dbJokes == null){
                Toast.makeText(getActivity(), "亲，你还没有收藏笑话哟！！", Toast.LENGTH_SHORT).show();
            } else {
                list.addAll(dbJokes);
                jokeAdapter.notifyDataSetChanged();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    private class MyReciver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            createData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(reciver);
    }
}
